/**
 * 获取搜索内容
 */

function getSearchArticle() {
	var articleName = $('#search').val();
	var seachArticleURL = getContextPath() + "/article/getsearcharticlebyarticlename";
	$.post(seachArticleURL, {
		"articleName" : articleName
	}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		alert(data);
	});
}


/*
 * 发表留言
 */

function reportStayMessage1() {
	var stayMessageArea = $('#stayMessageArea').val();
	$.post(getContextPath() + '/user/reportStayMessage', {
		"message_content" : stayMessageArea,
		"user_id" : 1
	}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		stay_Message();
		if (data == 1) {
			alert('留言成功')
			$('#stayMessageArea').val('');
		}
	});


}



/*
 * 显示留言信息
 */
function stay_Message() {
	// body...

	$.get(getContextPath() + '/user/stayMessage', function(data) {
		/*optional stuff to do after success */
		console.log(data);
		//var json = jQuery.parseJSON(data);
		var json = eval("(" + data + ")"); //转换成json对象
		alert(data);
		var trHTML = "";
		$("#list").html(""); //清空留言
		$('.messageboard').css({
			display : 'block'
		}); //显示留言
		for (var i = 0; i < json.length; i++) {
			var message = json[i];
			trHTML = "<tr><td>" + message.user_name + "</td>"
				+ "<td>" + message.message_content + "</td>"
				+ "<td>" + getLocalTime(message.message_stay_time) + "</td>" + "</tr>"
			$("tbody").append(trHTML);
		} //for结束
	}); //get结束
}

/*
 * 限制输入大小
 */
//todo
/*
window.onload = function() 
{ 
document.getElementById('stayMessageArea').onkeydown = function() 
{    
    if(this.value.length >= 10) 
      event.returnValue = false; 
} 
} 

*/

/*
 * java Date格式 转化
 */




function dateFtt(fmt, date) { //author: meizz   
	var o = {
		"M+" : date.getMonth() + 1, //月份   
		"d+" : date.getDate(), //日   
		"h+" : date.getHours(), //小时   
		"m+" : date.getMinutes(), //分   
		"s+" : date.getSeconds(), //秒   
		"q+" : Math.floor((date.getMonth() + 3) / 3), //季度   
		"S" : date.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function getLocalTime(nS) {
	var crtTime = new Date(nS);
	return top.dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);
}


function getContextPath() { //获取根路径

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}